/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.fatecpg.songs.intranet.managedbeans;

import br.com.fatecpg.songs.crud.facade.RegisterFacade;
import br.com.fatecpg.songs.dao.util.DAOException;
import br.com.fatecpg.songs.entity.Album;
import br.com.fatecpg.songs.util.FacesContextUtil;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.hibernate.exception.ConstraintViolationException;

/**
 *
 * @author Eloyza
 */
@ManagedBean
@ViewScoped
public class AlbumManagedBean implements RegisterManagedBean<Album>{
    private Album album;
    private RegisterFacade<Album> facade = new RegisterFacade<Album>(Album.class);
    private int viewState;
    private List<Album> list;

    public AlbumManagedBean() throws DAOException{
        album = new Album();                
        list = facade.list();
        this.viewState = LIST_STATE;
    }
            
    @Override
    public void openNew() {
        album = new Album();        
        this.viewState = REGISTER_STATE;
    }

    @Override
    public void openToUpdate() throws DAOException {        
        album = facade.load();
        this.viewState = REGISTER_STATE;
    }

    @Override
    public void delete() throws DAOException {
        try{
            facade.delete();
            list = facade.list();
            this.viewState = LIST_STATE;            
        }catch(ConstraintViolationException ex){
            FacesContextUtil.addMessage("Existem músicas neste album. Para deletá-lo, é necessário deletar as músicas.");            
            
        }
    
    }
       
    @Override
    public void register() throws DAOException {
        facade.setUpdateInfo(album);
        facade.register(album);
        list = facade.list();
        viewState = LIST_STATE;        
    }      
    
    @Override
    public void cancel() {
        album = new Album();
        this.viewState = LIST_STATE;        
    }

    public Album getAlbum() {
        return album;
    }

    public void setAlbum(Album album) {
        this.album = album;
    }

    public int getViewState() {
        return viewState;
    }

    public void setViewState(int viewState) {
        this.viewState = viewState;
    }   
    
    @Override
    public List<Album> getList() {
        return list;
    }

    public void setList(List<Album> list) {
        this.list = list;
    }   
}
